package com.andy.module.util

import android.text.InputFilter
import android.text.Spanned
import android.text.TextUtils
import java.util.regex.Pattern

/**
 * @creator:LuoFan
 * @date:2022/6/22
 * @description:
 */

class NumberInputFilter(//输入的最大金额
    private val mMaxValue: Double, //小数点后的位数
    private val mPointerLength: Int
) : InputFilter {
    private val mPattern: Pattern = Pattern.compile("([0-9]|\\.)*")

    //输入的最小值
    private var mMinValue = 0.0

    fun setMinValue(minValue: Double) {
        mMinValue = minValue
    }

    /**
     * @param source 新输入的字符串
     * @param start  新输入的字符串起始下标，一般为0
     * @param end    新输入的字符串终点下标，一般为source长度-1
     * @param dest   输入之前文本框内容
     * @param dstart 原内容起始坐标，一般为0
     * @param dend   原内容终点坐标，一般为dest长度-1
     * @return 输入de内容
     */
    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence {
        val sourceText = source.toString()
        val destText = dest.toString()
        //验证删除等按键
        if (TextUtils.isEmpty(sourceText)) {
            return ""
        }
        val matcher = mPattern.matcher(source)
        //已经输入小数点的情况下，只能输入数字
        if (destText.contains(POINTER)) {
            if (!matcher.matches()) {
                return ""
            } else {
                if (POINTER == source) {  //只能输入一个小数点
                    return ""
                }
            }
            //验证小数点精度，保证小数点后只能输入两位
            val index = destText.indexOf(POINTER)
            val length = destText.length - index
            //如果长度大于2，并且新插入字符在小数点之后
            if (length > mPointerLength && index < dstart) {
                //超出2位返回null
                return ""
            }
        } else {
            //没有输入小数点的情况下，只能输入小数点和数字，但首位不能输入小数点
            if (!matcher.matches()) {
                return ""
            } else {
                if (POINTER == source && TextUtils.isEmpty(destText)) {
                    return ""
                }
            }
        }
        //确保不会一直输入0
        val posDot = dest.toString().indexOf(".")
        if (start < end && posDot > 1 && dest.toString() == "0." && source == "0") { //确保不会出现不是小数的第一位为0
            return ""
        } else if (start < end && posDot < 0 && dest.toString() == "0" && !sourceText.contains(".")) {
            return ""
        }
        if (start < end && dstart == 0 && destText.length > 0 && source == "0") {
            return ""
        }
        try {
            //验证输入值大小 修改最大值验证
            var destValueStr = destText.substring(start, dend) + sourceText
            if (dend < destText.length) {
                destValueStr = destValueStr + destText.substring(dend)
            }
            val sumText = destValueStr.toDouble()
            //double sumText = Double.parseDouble(destText + sourceText);
            if (mMinValue > 0) {
                val content = destText + sourceText
                val minValueStr = mMinValue.toString() + ""
                if (content.length == minValueStr.length && sumText < mMinValue) {
                    return dest.subSequence(dstart, dend)
                }
            }
            if (sumText > mMaxValue) {
                return dest.subSequence(dstart, dend)
            }
        } catch (e: NumberFormatException) {
            return dest.subSequence(dstart, dend)
        }
        return dest.subSequence(dstart, dend).toString() + sourceText
    }

    companion object {
        private const val POINTER = "."
    }

}